package hu.veilingsite.gui;

import hu.veilingsite.client.Service;
import hu.veilingsite.client.ServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Registreeren {
	//alle onderdelen van de pagina
	private Label lblname, lbladdress, lblresidence, lblbirthdate, lblemail, lblpassword, lblpassword2;
	private TextBox boxname, boxaddress, boxresidence, boxbirthdateday, boxbirthdatemonth, boxbirthdateyear, boxemail;
	private PasswordTextBox boxpassword, boxpassword2;
	private Button registreer;


	public Registreeren(){
		//het aanmaken van de callback om dingen aan de server te vragen
		final ServiceAsync myService = (ServiceAsync)
		GWT.create(Service.class);
		final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
			}
			@Override
			public void onSuccess(Boolean result) {
				if(result){
					Window.alert("Uw account is succesvol aangemaakt.");
					RootPanel.get("main").clear();
					RootPanel.get("main").add(new Inloggen().createPage());
				}
				else{
					Window.alert("Er is iets mis gegaan bij het aanmaken van je acount, probeer het opnieuw.");
				}
			}
		};

		// het initialiseren van alle velden
		lblname = new Label("Uw naam:");
		lbladdress = new Label("Adres:");
		lblresidence = new Label("Woonplaats:");
		lblbirthdate = new Label("Geboortedatum (dd-mm-yyy):");
		lblemail = new Label("E-mail adres:");
		lblpassword = new Label("Wachtwoord:");
		lblpassword2 = new Label("Wachtwoord bevestigen:");

		boxname = new TextBox();
		boxaddress = new TextBox();
		boxresidence= new TextBox();
		boxbirthdateday = new TextBox(); boxbirthdateday.setVisibleLength(2);
		boxbirthdatemonth = new TextBox(); boxbirthdatemonth.setVisibleLength(2);
		boxbirthdateyear = new TextBox(); boxbirthdateyear.setVisibleLength(2);
		boxemail = new TextBox();
		boxpassword = new PasswordTextBox();
		boxpassword2 = new PasswordTextBox();

		registreer = new Button("Registreer");

		registreer.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent e){
				if(!boxpassword.getText().equals(boxpassword2.getText())){
					System.out.println("wachtwoorden zijn niet gelijk");
				}
				else{
					myService.registreer(boxname.getText(), boxpassword.getText(), boxemail.getText(), boxaddress.getText(), boxresidence.getText(), Integer.parseInt(boxbirthdateyear.getText()), Integer.parseInt(boxbirthdatemonth.getText()), Integer.parseInt(boxbirthdateday.getText()), callback);
				}
			}
		});
	}

	public Panel createPage(){
		VerticalPanel result = new VerticalPanel();

		HTML intro = new HTML();
		intro.setHTML("<h1>Registeren</h1>Met het onderstaande formulier kunt u zicht registeren op de website. Wanneer u een account aan heeft gemaakt kunt u uw eigen veilingen starten, of biedingen doen op lopende veilingen.<br /><br />");
		result.add(intro);

		HorizontalPanel p4 = new HorizontalPanel();
		boxbirthdateday.addStyleName("sadbox");
		p4.add(boxbirthdateday);
		boxbirthdatemonth.addStyleName("sadbox");
		p4.add(boxbirthdatemonth);
		boxbirthdateyear.addStyleName("sadbox");
		p4.add(boxbirthdateyear);

		FlexTable table = new FlexTable();
		table.addStyleName("nicetable");

		table.setWidget(0,  0, lblname);
		table.setWidget(0,  1, boxname);
		table.setWidget(1,  0, lbladdress);
		table.setWidget(1,  1, boxaddress);
		table.setWidget(2,  0, lblresidence);
		table.setWidget(2,  1, boxresidence);
		table.setWidget(3,  0, lblbirthdate);
		table.setWidget(3,  1, p4);
		table.setWidget(4,  0, lblemail);
		table.setWidget(4,  1, boxemail);
		table.setWidget(5,  0, lblpassword);
		table.setWidget(5,  1, boxpassword);
		table.setWidget(6,  0, lblpassword2);
		table.setWidget(6,  1, boxpassword2);

		table.setWidget(7,  1, registreer);

		result.add(table);

		return result;
	}
}
